import { makeAutoObservable } from "mobx";

class UserStore {
  /* 数据 ----------------- */
  // 用户信息
  userInfo = null;

  // 登录时间
  loginTime = null;

  // token
  token = null;

  constructor() {
    makeAutoObservable(this); // 响应式处理
  }

  /* 方法 ----------------- */
  // 清空登录信息
  clearLoginInfo = () => {
    this.userInfo = null;
    this.loginTime = null;
    this.token = null;
    localStorage.clear();
    sessionStorage.clear();
  };

  // 修改用户信息
  setUserInfo = (userInfo) => {
    this.userInfo = userInfo;
    localStorage.setItem("userInfo", JSON.stringify(userInfo));
  };

  // 修改登录时间
  setLoginTime = (time) => {
    this.loginTime = time;
    localStorage.setItem("loginTime", time);
  };

  // 修改token
  setToken = (token) => {
    this.token = token;
    localStorage.setItem("token", token);
  };
}

const userStore = new UserStore();
export default userStore;
